package com.ydy.biz;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ydy.mapper.Account;
import com.ydy.mapper.Addr;
import com.ydy.mapper.User;
import com.ydy.service.AccountService;
import com.ydy.service.AddrService;
import com.ydy.service.UserService;
import com.ydy.vo.UserVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
@RequiredArgsConstructor
public class UserBizImpl implements UserBiz {

    @Autowired
    private UserService userService;
    @Autowired
    private AddrService addrService;
    @Autowired
    private AccountService accountService;

    private final StringRedisTemplate stringRedisTemplate;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addUser(User user) {
        userService.save(user);
    }
    @Override
    public List<User> findAll(){
        return userService.list();
    }

    @Override
    public void updateUserByName(User user) {
        if(user.getId() == null){
            throw new RuntimeException("id不能为空");
        }
        User byId = userService.getById(user.getId());
        if(byId!= null){
            byId.setName(user.getName());
            userService.updateById(byId);
        }
    }

    @Override
    public void deleteById(Integer id) {
        userService.removeById(id);
    }

    @Override
    public UserVo getUserVo(Integer id) {
        User user = userService.getById(id);
        if(user == null){
            throw new RuntimeException("用户不存在");
        }
        UserVo userVo = new UserVo();
        userVo.setId(user.getId());
        userVo.setName(user.getName());

        Addr addr = addrService.getOne(new LambdaQueryWrapper<Addr>().eq(Addr::getUserId, id).last("limit 1"));//select * from addr where user_id = ? limit 1;
        if (addr!= null){
            userVo.setAddr(addr.getAddr());
        }
        Account one = accountService.getOne(new LambdaQueryWrapper<Account>().eq(Account::getUserId, id).last("limit 1"));
        if(one!= null){
            userVo.setAccount(one.getAccount());
        }
        return userVo;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addUserInfo(UserVo userVo) {
        User user = new User();
        user.setName(userVo.getName());
        userService.save(user);//新增成功的user有id
        log.info("user id:{}", user.getId());
        Addr addr = new Addr();
        addr.setAddr(userVo.getAddr());
        addr.setUserId(user.getId());
        addrService.save(addr);
        Account account = new Account();
        account.setAccount(userVo.getAccount());
        account.setUserId(user.getId());
        accountService.save(account);
        log.info("userVo:{}新增成功", userVo);
    }

    /**
     * user:1
     */
    public static String userKey = "user:";

    @Override
    public UserVo getUserVoWithRedis(Integer id) {
        String s = stringRedisTemplate.opsForValue().get(userKey + id);
        if (!StringUtils.isEmpty(s)){
            UserVo userVo = JSON.parseObject(s, UserVo.class);
            return userVo;
        }
        UserVo userVo = this.getUserVo(id);
        stringRedisTemplate.opsForValue().set(userKey + id, JSON.toJSONString(userVo),10, TimeUnit.SECONDS);
        try {
            Thread.sleep(1000); // 等待1秒
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return userVo;
    }


}
